﻿<%@ WebHandler Language="C#" Class="SpecDurationPerUnitReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 待维护工时查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.5.13
 **/

public class SpecDurationPerUnitReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getSearchSqlString(JObject jObject)
    {
        string sql = @"
SELECT f.factoryname,
       pb.productname,pb.productname || ':' || p.productrevision || '(' ||pt.producttypename || ')' productinfo,
       wfb.workflowname || ':' || wf.workflowrevision || '(' ||wf.workflowtype || ')' workflowinfo,wf.description wfdesc,
       e.fullname,s.specno,s.opdesc,'' durationperunit,cts.lastchangedate
FROM product p
LEFT JOIN productbase pb ON p.productbaseid = pb.productbaseid
LEFT JOIN workflowbase wfb1 ON wfb1.workflowbaseid = p.workflowbaseid
LEFT JOIN producttype pt ON pt.producttypeid = p.producttypeid
LEFT JOIN workflow wf1 ON wf1.workflowid = nvl(wfb1.revofrcdid, p.workflowid)
LEFT JOIN workflowbase wfb ON wfb.workflowbaseid = wf1.workflowbaseid
LEFT JOIN workflow wf ON wf.workflowbaseid = wfb.workflowbaseid
LEFT JOIN factory f ON f.factoryid = wf.factoryid
INNER JOIN 
(
    SELECT ws.workflowid 
    FROM workflowstep ws
    LEFT JOIN workflow wf ON wf.workflowid = ws.workflowid
    LEFT JOIN stepschedulingdetail sd ON sd.stepid = ws.workflowstepid
    LEFT JOIN (
         SELECT sif.workflowstepid,sif.stepinfoid,sif.durationperunit 
         FROM stepinfo sif  
         INNER JOIN stepinfobase sib ON sib.revofrcdid=sif.stepinfoid 
     ) SIO on sio.workflowstepid=ws.workflowstepid 
     WHERE (( sio.stepinfoid is  NULL and sd.stepid IS NOT NULL AND sd.durationperunit IS NULL) or (sio.stepinfoid is not NULL AND sio.durationperunit IS NULL)) ";

        //主制分厂
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND wf.factoryid = '{0}'", jObject["factoryid"].ToString());
        }

        sql += @"
           AND ws.sequence IS NOT NULL 
     GROUP BY ws.workflowid
) WFID ON WFID.workflowid = wf.workflowid
LEFT JOIN Workflowstep ws ON ws.workflowid = wf.workflowid
LEFT JOIN employee e ON e.employeeid = wf.zgemployeeid
LEFT JOIN specbase sb ON sb.specbaseid = ws.specbaseid
LEFT JOIN spec s ON s.specid = NVL(sb.revofrcdid,ws.specid)
LEFT JOIN Changestatus cts ON cts.parentid = wf.workflowid
WHERE 1=1 
";

        //件号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND pb.productname = '{0}'", jObject["productname"].ToString());
        }

        //工艺路线
        if (jObject["workflowname"] != null && jObject["workflowname"].ToString() != "")
        {
            sql += string.Format(" AND wfb.workflowname LIKE '%{0}%'", jObject["workflowname"].ToString());
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        Result r = new Result();

        string sql = getSearchSqlString(jObject);

        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);

        DataTable dt = dto.DBTable;

        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getSearchSqlString(jObject);
        DataTable dtResult = dal.GetDataTable(sql);

        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        dtResult.Columns["factoryname"].Caption = "主制分厂";
        dtResult.Columns["productname"].Caption = "零件图号";
        dtResult.Columns["productinfo"].Caption = "件号版本";
        dtResult.Columns["workflowinfo"].Caption = "工艺路线版本";
        dtResult.Columns["wfdesc"].Caption = "工艺路线名称";
        dtResult.Columns["specno"].Caption = "工序号";
        dtResult.Columns["opdesc"].Caption = "工序名称";
        dtResult.Columns["durationperunit"].Caption = "单位工时";
        dtResult.Columns["fullname"].Caption = "主管工艺";
        dtResult.Columns["lastchangedate"].Caption = "推送时间";
        ExportHelper.ExportExcel(dtResult, "待维护工时查询报表");
    }
}